import torch
import torch.nn as nn
import torch.nn.functional as F

class ConvolutionQnet(torch.nn.Module):
    def __init__(self, input_shape, action_dim, in_channels=4):
        super(ConvolutionQnet, self).__init__()

        self.input_shape = input_shape
        self.features = torch.nn.Sequential(
            nn.Conv2d(in_channels, 32, kernel_size=8, stride=4),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=4, stride=2),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=1),
            nn.ReLU()
        )

        self.fc = nn.Sequential(
            nn.Linear(self.feature_size, 512),
            nn.ReLU(),
            nn.Linear(512, action_dim)
        )

    def forward(self, x):
        x = self.features(x).view(x.size()[0], -1)
        return self.fc(x)

    @property
    def feature_size(self):
        x = self.features(torch.zeros(1, *self.input_shape))
        return x.view(1, -1).size(1)